
/*加载方法*/
$(function (){
    menuInfo();
})

//菜单信息渲染
function menuInfo(){
    $.ajax({
        url:"/Role/byRoleId",
        type:"get",
        dataType:"json",
        success:function (data){
            //将data中的集合和对象取出来
            let menuList=data.menuList;
            let menuList1=data.menuList;
            let menuList2=data.menuList;
            let menuList3=data.menuList;
            let role=data.user.role;
            let user=data.user;

            let li="";
            /*循环菜单*/
            for (let i = 0; i < menuList.length; i++) {
                li+="<li class=\"nav-item\">";
                if (menuList[i].parentId==0 && menuList[i].menuGrade==1){
                    li+= "\t\t\t\t\t\t\t<a data-toggle=\"collapse\" href='#submenu"+menuList[i].menuId+"'>\n" +
                        "\t\t\t\t\t\t\t\t<i class='fas "+menuList[i].menuIcon+"' ></i>\n" +
                        "\t\t\t\t\t\t\t\t<p>"+menuList[i].menuName+"</p>\n" +
                        "\t\t\t\t\t\t\t\t<span class=\"badge badge-count\">5</span>\n" +
                        "\t\t\t\t\t\t\t\t<span class=\"caret\"></span>\n" +
                        "\t\t\t\t\t\t\t</a>";

                    li+="<div class=\"collapse\" id='submenu"+menuList[i].menuId+"'>\n" +
                        "\t\t\t\t\t\t\t\t<ul class=\"nav nav-collapse\">\n";
                    for (let j = 0; j < menuList1.length; j++) {

                        if (menuList1[j].parentId==menuList[i].menuId && menuList1[j].menuGrade==2
                            && (menuList1[j].menuRul=='')){
                            li+= "\t\t\t\t\t\t\t\t\t<li>\n" +
                                "\t\t\t\t\t\t\t\t\t\t<a data-toggle=\"collapse\" href='#subnav"+menuList1[j].menuId+"'>\n" +
                                "\t\t\t\t\t\t\t\t\t\t\t<span class=\"sub-item\">"+menuList1[j].menuName+"</span>\n" +
                                "\t\t\t\t\t\t\t\t\t\t\t<span class=\"caret\"></span>\n" +
                                "\t\t\t\t\t\t\t\t\t\t</a>";

                            li+="<div class=\"collapse\" id='subnav"+menuList1[j].menuId+"'>\n" +
                                "\t\t\t\t\t\t\t\t\t\t\t<ul class=\"nav nav-collapse subnav\">";

                            for (let k = 0; k < menuList2.length; k++) {

                                if (menuList2[k].parentId==menuList1[j].menuId && menuList2[k].menuGrade==3
                                    && (menuList2[k].menuRul=='')){

                                    li+="<li>\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t<a data-toggle=\"collapse\" href='#subul"+menuList2[k].menuId+"' target=\"right\">\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"sub-item\">"+menuList2[k].menuName+"</span>\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"caret\"></span>\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t</a>";

                                    li+="<div class=\"collapse\" id='subul"+menuList2[k].menuId+"'>\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<ul class=\"nav nav-collapse subul\">";

                                    for (let l = 0; l < menuList3.length; l++) {

                                        if (menuList3[l].parentId==menuList2[k].menuId && menuList3[l].menuGrade==4){
                                            li+="<li value='"+menuList[i].menuName+"&"+menuList1[j].menuName+"&"+menuList2[k].menuName+"'>\n" +
                                                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<a onclick='selectedLi(this)' href='"+menuList3[l].menuRul+"' target=\"right\">\n" +
                                                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"sub-item\">"+menuList3[l].menuName+"</span>\n" +
                                                "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</a>";
                                        }
                                        li+="</li>";
                                    }
                                    li+="</ul>\n" +
                                        "\t\t\t\t\t\t\t\t\t\t\t\t\t</div>";
                                }else{
                                    if (menuList2[k].parentId==menuList1[j].menuId && menuList2[k].menuGrade==3){
                                        li+="<li value='"+menuList[i].menuName+"&"+menuList1[j].menuName+"'>\n" +
                                            "\t\t\t\t\t\t\t\t\t\t\t\t\t<a onclick='selectedLi(this)' href='"+menuList2[k].menuRul+"' target=\"right\">\n" +
                                            "\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"sub-item\">"+menuList2[k].menuName+"</span>\n" +
                                            "\t\t\t\t\t\t\t\t\t\t\t\t\t</a>";
                                    }
                                }
                                li+="</li>";
                            }
                            li+="</ul>\n" +
                                "\t\t\t\t\t\t\t\t\t\t</div>";
                        }else{
                            if (menuList1[j].parentId==menuList[i].menuId && menuList1[j].menuGrade==2){
                                li+="<li value='"+menuList[i].menuName+"'>\n" +
                                    "\t\t\t\t\t\t\t\t\t\t<a onclick='selectedLi(this)' href='"+menuList1[j].menuRul+"' target=\"right\">\n" +
                                    "\t\t\t\t\t\t\t\t\t\t\t<span class=\"sub-item\">"+menuList1[j].menuName+"</span>\n" +
                                    "\t\t\t\t\t\t\t\t\t\t</a>";
                            }
                        }
                        li+="</li>";
                    }
                    li+="</ul>\n" +
                        "\t\t\t\t\t\t\t</div>";

                }

                li+="</li>";
            }

            //获取menuId,将菜单加入到元素中
            $("#menuId").append(li);

            //将账号信息显示
            $(".user_account").html(user.userAccount);
            $(".user_img").attr("src","../assets/img/"+user.userImg);
            $(".user_role").html(role.roleName);
            $(".user_name").html(user.userName);
            $(".user_id").html(user.userId);
        }
    })

}

//选中当前点击跳转的页面,并且将导航显示
function selectedLi(that){
    $(".nav-item").removeClass("active");
    $(".nav-item li").removeClass("active");
    $(that).parent().addClass("active");
    $(".page-title").html($(that).find("span").html());

    let text = $(that).parent().attr("value");
    let split = text.split("&");

    let bread="<li class=\"nav-home\">\n" +
        "\t\t\t\t\t\t\t\t<a href=\"#\">\n" +
        "\t\t\t\t\t\t\t\t\t<i class=\"flaticon-home\"></i>\n" +
        "\t\t\t\t\t\t\t\t</a>\n" +
        "\t\t\t\t\t\t\t</li>";
    for (let i = 0; i < split.length; i++) {
        bread+="<li class=\"separator\">\n" +
            "\t\t\t\t\t\t\t\t<i class=\"flaticon-right-arrow\"></i>\n" +
            "\t\t\t\t\t\t\t</li>\n" +
            "\t\t\t\t\t\t\t<li class=\"nav-item\">\n" +
            "\t\t\t\t\t\t\t\t<a href=\"#\">"+split[i]+"</a>\n" +
            "\t\t\t\t\t\t\t</li>";
    }
    bread+="<li class=\"separator\">\n" +
        "\t\t\t\t\t\t\t\t<i class=\"flaticon-right-arrow\"></i>\n" +
        "\t\t\t\t\t\t\t</li>\n" +
        "\t\t\t\t\t\t\t<li class=\"nav-item\">\n" +
        "\t\t\t\t\t\t\t\t<a href=\"#\">"+$(that).find("span").html()+"</a>\n" +
        "\t\t\t\t\t\t\t</li>";
    $(".breadcrumbs").html(bread);
}


//变背景色
function changeBgColor(bgColor){
    var x=document.getElementById("iframeId");
    var y=(x.contentWindow || x.contentDocument);
    if (y.document)y=y.document;
    y.body.style.backgroundColor=bgColor;

}